﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Collections
Imports System.Windows.Forms
Imports DataObject
Imports System.Data.SqlClient

Public Class SqlServerLoaiHopDongLD

    Private oqLBase As New hDBase
    Private oDataSet As New DataSet
    Private oDatatable As New DataTable

    Public Function GetAllLoaiHopDongLD(ByVal sConnString As String) As DataTable
        oqLBase.Connection(sConnString)
        oDataSet = oqLBase.GetData("Get_All_LoaiHopDongLDs", "LoaiHopDongLD")
        Return oDataSet.Tables("LoaiHopDongLD")
    End Function

    Public Function InsertLoaiHopDongLD(ByVal sConnString As String, ByVal oLoaiHopDongLD As LoaiHopDongLD) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoLoaiHDLD", SqlDbType.Int), _
                                           New SqlParameter("@MaLoaiHDLD", SqlDbType.VarChar, 20), _
                                           New SqlParameter("@TenLoaiHDLD", SqlDbType.NVarChar, 50), _
                                           New SqlParameter("@HoatDong", SqlDbType.Bit)}
        SqlParam(0).Direction = ParameterDirection.Output
        SqlParam(1).Value = oLoaiHopDongLD.MaLoaiHDLD
        SqlParam(2).Value = oLoaiHopDongLD.TenLoaiHDLD
        SqlParam(3).Value = oLoaiHopDongLD.HoatDong
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Insert_LoaiHopDongLD", "LoaiHopDongLD", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function UpdateLoaiHopDongLD(ByVal sConnString As String, ByVal oLoaiHopDongLD As LoaiHopDongLD) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoLoaiHDLD", SqlDbType.Int), _
                                    New SqlParameter("@MaLoaiHDLD", SqlDbType.VarChar, 20), _
                                    New SqlParameter("@TenLoaiHDLD", SqlDbType.NVarChar, 50), _
                                    New SqlParameter("@HoatDong", SqlDbType.Bit)}
        SqlParam(0).Value = oLoaiHopDongLD.SoLoaiHDLD
        SqlParam(1).Value = oLoaiHopDongLD.MaLoaiHDLD
        SqlParam(2).Value = oLoaiHopDongLD.TenLoaiHDLD
        SqlParam(3).Value = oLoaiHopDongLD.HoatDong
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Update_LoaiHopDongLD", "LoaiHopDongLD", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function DeleteLoaiHopDongLD(ByVal sConnString As String, ByVal SoLoaiHopDongLD As Integer) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoLoaiHopDongLD", SqlDbType.Int)}
        SqlParam(0).Value = SoLoaiHopDongLD
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Delete_LoaiHopDongLD", "LoaiHopDongLD", SqlParam)
            Return True
        Catch ex As SqlException
            Select Case ex.ErrorCode
                Case -2146232060
                    MessageBox.Show("Không thể xóa dữ liệu này. Loại hợp đồng lao động này đang được sử dụng trong phần nghiệp vụ khác", "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return False
            End Select
        End Try
    End Function

End Class
